/**
 * @file app_main.c
 * @brief 
 * @author yejing (yejh@gongniu.cn)
 * @version 1.0
 * @date 2024-05-15
 * 
 * Copyright 2024-2025 GongNiu Inc. All Rights Reserved.
 * 
 */

#include <stdio.h>

#include "bull_hal_sys_thread.h"
#include "bull_hal_sys_timer.h"
#include "bull_hal_sys_system.h"
#include "bull_hal_drv_wifi.h"
#include "bull_hal_net_mqtt.h"
#include "bull_hal_sys_memory.h"
#include "esp_log.h"

static const char* MQTT_TEST = "mqtt_test";
static MQTT_CLIENT_HANDLE client_hadnle = NULL;
static MQTT_CLIENT_HANDLE client_hadnle1 = NULL;
/**
 * @brief 
 * 
 * @param param 
 */
static void app_init_thread(void* param)
{
    BULL_HAL_MQTT_REGISTER_T register_params = { 
        .uri = "mqtt://debug.iotbull.com",
        .port = 1883,
        .client_id = "test1",
        .username = "Bull",
        .password = "Bull@123"
    };

    bool first_init = true;
    uint16_t cnt = 0;

    while ( 1 ){
        
        bull_hal_sys_sleep( 1000 );

        NW_IP_S ip = { 0 };
        bull_hal_drv_wifi_get_ip( WF_STATION, &ip );
        ESP_LOGI( MQTT_TEST,"ip:%s gw:%s mask:%s", ip.ip, ip.gw, ip.mask );

        if ( strlen( ip.mask) != 0 && true == first_init )
        {
           first_init = false;
           ESP_LOGI( MQTT_TEST, "mqtt init" );
           bull_hal_net_mqtt_init( &client_hadnle, &register_params );
           bull_hal_net_mqtt_client_start( client_hadnle );

           register_params.client_id = bull_hal_sys_memory_malloc( 20 );
           strncpy( register_params.client_id, "test2", strlen( "test2" ) );
           bull_hal_net_mqtt_init( &client_hadnle1, &register_params );
           bull_hal_net_mqtt_client_start( client_hadnle1 );

            ESP_LOGI( MQTT_TEST,"bull_hal_net_mqtt_client_stop:%d",bull_hal_net_mqtt_client_stop(client_hadnle1) );
            ESP_LOGI( MQTT_TEST,"bull_hal_net_mqtt_client_start:%d",bull_hal_net_mqtt_client_start(client_hadnle1) );

            bull_hal_sys_memory_free( register_params.client_id );
        }
        
        if( NULL != client_hadnle )
        {
            ESP_LOGI( MQTT_TEST,"test" );
            bull_hal_net_mqtt_client_pub( client_hadnle, "test/test", (uint8_t *)"test",strlen( "test" ), 0, 0 );
        }
        
        if( NULL != client_hadnle1 )
        {
            ESP_LOGI( MQTT_TEST,"test1" );
            bull_hal_net_mqtt_client_pub( client_hadnle1, "test/test", (uint8_t *)"test1",strlen( "test1" ), 0, 0 );
        }else{
            ESP_LOGI( MQTT_TEST,"client_hadnle1 is null" );
        }

        ESP_LOGE( MQTT_TEST, "cnt:%d", cnt );

        if ( cnt ++ > 15 && client_hadnle1 )
        {
            if( OPRT_OK == bull_hal_net_mqtt_client_destroy( client_hadnle1 ) ){

                ESP_LOGI( MQTT_TEST,"bull_hal_net_mqtt_client_destroy success" );
                client_hadnle1 = NULL;
            }

        }
    }
}



/**
 * @brief 
 * 
 */
static void app_init_entry(void)
{

    bull_hal_drv_wifi_init( NULL );

    // wifi作为sta模式连接路由器
    bull_hal_drv_wifi_station_connect((const char *)"GN-XIU",(const char *)"88888888");

    bull_hal_sys_thread_create( app_init_thread,
    "app_init_thread",
    4*4096 ,
    NULL,
    10,
    NULL);

}

/**
 * @brief 
 * 
 */
void app_main(void)
{

    app_init_entry();

}
